# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=graphviz
pkgver=9.0.0
pkgrel=1
pkgdesc="Graph Visualization Tools"
url="https://www.graphviz.org/"
arch="all"
license="EPL-1.0"
options="!check" # fixme: enable testsuite
_luaver=5.4
depends_dev="
	cairo-dev
	expat-dev
	fontconfig-dev
	freetype-dev
	gd-dev
	gmp-dev
	libjpeg-turbo-dev
	libpng-dev
	libsm-dev
	libxext-dev
	pango-dev
	python3-dev
	zlib-dev
	"
makedepends="
	$depends_dev
	bison
	flex
	libltdl
	libxaw-dev
	lua$_luaver-dev
	m4
	swig
	tcl
	"
install="$pkgname.pre-deinstall"
triggers="$pkgname.trigger=/usr/lib/graphviz"
subpackages="
	$pkgname-dev
	$pkgname-libs
	$pkgname-doc
	py3-gv:_py3
	lua$_luaver-$pkgname:_lua
	$pkgname-graphs::noarch
	"
source="$pkgname-$pkgver.tar.xz::https://gitlab.com/api/v4/projects/4207231/packages/generic/graphviz-releases/$pkgver/graphviz-$pkgver.tar.xz"

# secfixes:
#   2.46.0-r0:
#     - CVE-2020-18032

prepare() {
 	default_prepare
	update_config_sub
}

build() {
	LIBPOSTFIX=/ \
	LUA=lua$_luaver \
	lua_suffix=$_luaver \
	CFLAGS="$CFLAGS -O2 -flto=auto" \
	CXXFLAGS="$CXXFLAGS -O2 -flto=auto" \
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--disable-python \
		--disable-silent-rules \
		--disable-static \
		--disable-dependency-tracking \
		--disable-ltdl-install \
		--enable-ltdl \
		--enable-sharp=no \
		--enable-go=no \
		--enable-guile=no \
		--enable-java=no \
		--enable-lua=yes \
		--enable-ocaml=no \
		--enable-perl=no \
		--enable-php=no \
		--enable-python3 \
		--enable-r=no \
		--enable-ruby=no \
		--enable-tcl=no \
		--without-included-ltdl \
		--with-gdk-pixbuf=yes \
		--with-ipsepcola=yes \
		--with-jpeg \
		--with-libgd=yes \
		--with-pangocairo=yes \
		--with-rsvg=yes \
		--with-x

	make
}

package() {
	# Install has race conditions...
	make DESTDIR="$pkgdir" \
		pkgconfigdir=/usr/lib/pkgconfig \
		install
}

_py3() {
	pkgdesc="Python3 extension for graphviz"
	depends="python3"

	amove usr/lib/graphviz/python3* \
		usr/lib/python3*
}

_lua() {
	pkgdesc="Lua$_luaver extension for graphviz"
	provides="lua-$pkgname=$pkgver-r$pkgrel"  # for backward compatibility
	replaces="lua-$pkgname"  # for backward compatibility

	mkdir -p "$subpkgdir"/usr/lib/lua
	mv "$pkgdir"/usr/lib/lua "$subpkgdir"/usr/lib/lua/$_luaver
	amove usr/lib/graphviz/lua
}

graphs() {
	pkgdesc="Demo graphs for graphviz"

	amove usr/share/graphviz/graphs
}

sha512sums="
912f31a4a90e21de9601ffcf8a85d2650d46f07e61aeb0e5124e5a91c67f444d08d40d49f15f2e4078d53d01fb9ca6f54dc9d18c1c08406da91b8ba9ad61416c  graphviz-9.0.0.tar.xz
"
